<?php
include_once $REP_OUTILS . 'Selector.php';
include_once $REP_OUTILS . 'MyException.php';
// Pour FirePHP
include_once($REP_LIB . 'FirePHPCore/FirePHP.class.php');


/**
 * PariGest - class.Pari.php
 *
 * $Id$
 *
 * This file is part of PariGest.
 *
 * Automatically generated on 06.08.2011, 21:02:14 with ArgoUML PHP module 
 * (last revised $Date: 2010-01-12 20:14:42 +0100 (Tue, 12 Jan 2010) $)
 *
 * @author Séb
 */

if (0 > version_compare(PHP_VERSION, '5')) {
    die('This file was generated for PHP 5');
}

/**
 * include Compte
 *
 * @author firstname and lastname of author, <author@example.org>
 */
require_once('class.Compte.php');

/**
 * include Operation
 *
 * @author firstname and lastname of author, <author@example.org>
 */
require_once('class.Operation.php');

/* user defined includes */


/* user defined constants */
// section 127-0-0-1-5eda895a:130ae729f01:-8000:000000000000086A-constants begin
// section 127-0-0-1-5eda895a:130ae729f01:-8000:000000000000086A-constants end

/**
 * Short description of class Pari
 *
 * @access public
 * @author Séb
 */
class Pari
{
  
  /** Résultat gagné */
  public static $IND_RESULTAT_GAGNE  = 'G';
  /** Résultat perdu */
  public static $IND_RESULTAT_PERDU  = 'P';
  /** Résultat annulé */
  public static $IND_RESULTAT_ANNULE = 'A';
  
  /** le pari est un combiné */
  public static $IND_COMBINE_OUI = 'O';
  /** Le pari n'est pas un combiné */
  public static $IND_COMBINE_NON = 'N';
  
  /** Indicateur de sport de foot */
  public static $IND_SPORT_FOOT   = 'F';
  private static $IND_SPORT_FOOT_LIB = 'Foot';
  /** Indicateur de sport de rugby */
  public static $IND_SPORT_RUGBY  = 'R';
  private static $IND_SPORT_RUGBY_LIB = 'Rugby';
  /** Indicateur de sport de tennis */
  public static $IND_SPORT_TENNIS = 'T';
  private static $IND_SPORT_TENNIS_LIB = 'Tennis';
  /** Indicateur de sport de BasketBall */
  public static $IND_SPORT_BASKET = 'B';
  private static $IND_SPORT_BASKET_LIB = 'BasketBall';
  
  public static $IND_SPORT_SESSION = "PARIGEST_INDSPORT";
  public static $COTE_SESSION      = "PARIGEST_COTE";
  
    // --- ASSOCIATIONS ---
    // generateAssociationEnd :     // generateAssociationEnd :     // generateAssociationEnd :     // generateAssociationEnd : 

    // --- ATTRIBUTES ---

    
    /**
    * Short description of attribute IdOperation
     *
    * @access private
    * @var Integer
    */
    private $IdOperation = null;

    /**
     * Short description of attribute IdCompte
     *
     * @access private
     * @var Integer
     */
    private $IdCompte = null;

    /**
     * Short description of attribute Date
     *
     * @access private
     * @var Date
     */
    private $Date = null;

    /**
     * Short description of attribute IndSport
     *
     * @access private
     * @var String
     */
    private $IndSport = null;

    /**
     * Short description of attribute Mise
     *
     * @access private
     * @var UnlimitedInteger
     */
    private $Mise = null;

    /**
     * Short description of attribute Cote
     *
     * @access private
     * @var UnlimitedInteger
     */
    private $Cote = null;
    /**
    * Short description of attribute Cote
    *
    * @access private
    * @var UnlimitedInteger
    */
    private $SoldeVirtuel = null;
    
    /**
     * Short description of attribute IndResultat
     *
     * @access private
     * @var String
     */
    private $IndResultat = null;

    /**
     * Short description of attribute IndCombine
     *
     * @access public
     * @var String
     */
    public $IndCombine = null;

    /**
     * Short description of attribute NbPari
     *
     * @access public
     * @var Integer
     */
    public $NbPari = null;

    /**
     * Short description of attribute Commentaire
     *
     * @access public
     * @var String
     */
    public $Commentaire = null;

    // --- OPERATIONS ---

    function __construct($db)
    {
      // Création de la connexion
      $this->maConnexion = $db;
      // Initialisation des requêtes
      $this->initRequete();
      // Préparation des requêtes
      $this->prepare();
      $this->trace = FirePHP::getInstance(true);
    }
    
    /**
    * Initialisation des requêtes préparées
    */
    private function initRequete(){
    
      $this->sqlInsert = "INSERT INTO Pari (IdOperation, IdCompte, Date, IndSport, Mise, Cote, IndResultat, IndCombine, NbPari, Commentaire, SoldeVirtuel) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
      $this->sqlDelete = "DELETE FROM Pari WHERE `idOperation` = ? and `IdCompte`=?";
      $this->sqlUpdate = "UPDATE Pari SET Date = ?, IndSport = ?, Mise = ?, Cote = ?, IndResultat = ?, IndCombine = ?, NbPari = ?, Commentaire = ?, SoldeVirtuel = ? WHERE `IdOperation` = ? and `IdCompte`=?";
    
    }
    /**
     * Préparation des requêtes
     */
    private function prepare()
    {    
      $this->reqInsert       = $this->maConnexion->prepare($this->sqlInsert);
      $this->reqDelete       = $this->maConnexion->prepare($this->sqlDelete);
      $this->reqUpdate       = $this->maConnexion->prepare($this->sqlUpdate);
    
    }

    /**
     * Short description of method affichage
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function affichage()
    {
      $this->trace->fb('Pari->affichage() : ' . $this->toStringClePrimaire());
      $user = new Pari($this->maConnexion);
      $user->setIdOperation($this->getIdOperation());
      $user->setIdCompte($this->getIdCompte());
      // Lecture de la première ligne
      $stmt = $user->select();
      $result = $stmt->fetch();

      $this->IdOperation = $result['IdOperation'];
      $this->IdCompte    = $result['IdCompte'];
      $this->Date        = $result['Date'];
      $this->IndSport    = $result['IndSport'];
      $this->Mise        = $result['Mise'];
      $this->Cote        = $result['Cote'];
      $this->SoldeVirtuel= $result['SoldeVirtuel'];
      $this->IndResultat = $result['IndResultat'];
      $this->IndCombine  = $result['IndCombine'];
      $this->NbPari      = $result['NbPari'];
      $this->Commentaire = $result['Commentaire'];

      // Fermeture du curseur
      $stmt->closeCursor();
    }

    public function toStringClePrimaire()
    {
      return '|' . $this->IdOperation . '|'. $this->IdCompte . '|';
    }
    
    /**
     * Short description of method insertion
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function insertion()
    {
      $this->trace->fb('Pari.insertion : ' . $this->toStringClePrimaire());
      
      try
      {
        $this->ajouteOperation();
        //$this->updateSoldeVirtuel();
        $this->setSoldeVirtuel($this->selectMiseConseillee( $this->Cote ));
        
        // Requete d'insertion
        $champs = array($this->IdOperation, $this->IdCompte, $this->Date, $this->IndSport, $this->Mise, $this->Cote, $this->IndResultat, $this->IndCombine, $this->NbPari, $this->Commentaire, $this->SoldeVirtuel);
        $this->reqInsert->execute($champs);
        
//      return $this->maConnexion->lastInsertId();
        
      }
      catch(Exception $e)
      {
        //$this->trace->fb('Erreur : '.($this->reqInsert->errorInfo())[1]);
         //echo 'Erreur : <br>'; print_r($this->reqInsert->errorInfo());
         throw new MyException('Une erreur est survenue lors de l\'insertion du pari.', $this->reqInsert->errorInfo());
      }
    }

    /**
     * Short description of method modification
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function modification()
    {
      $this->trace->fb('modification : ' . $this->toStringClePrimaire());
            
      try
      {
        // Chargement des infos avant modification du  pari
        /*$pari = new Pari($this->maConnexion);
        $pari->setIdCompte($this->getIdCompte());
        $pari->setIdOperation($this->getIdOperation());
        $pari->affichage();
        
        // Modification du solde virtuel
        $compte = new Compte($this->maConnexion);
        $compte->setIdCompte($this->getIdCompte());
        $compte->affichage();
        
        // Calcul du solde virtuel
        $compte->setSoldeVirtuel($compte->getSoldeVirtuel() - $pari->getMontantOperation() + $this->getMontantOperation());        
        $compte->modification();*/
        
        // Requete de modification
        $champs = array($this->Date, $this->IndSport, $this->Mise, $this->Cote, $this->IndResultat, $this->IndCombine, $this->NbPari, $this->Commentaire, $this->SoldeVirtuel, $this->IdOperation, $this->IdCompte);
        
        // Modification du pari
        $retour = $this->reqUpdate->execute($champs);
                
        $operation = new Operation($this->maConnexion);
        $operation->setIdCompte($this->getIdCompte());
        $operation->setIdOperation($this->getIdOperation());
        $operation->affichage();
        $operation->setMontant($this->getMontantOperation());
        $operation->modification();        
        
        return $retour;
      }
      catch(Exception $e)
      {
        //echo 'Erreur : <br>'; print_r($this->reqUpdate->errorInfo());
        throw new MyException('Une erreur est survenue lors de la modification du pari.', $this->reqUpdate->errorInfo());
      }     
    }

    /**
     * Short description of method suppression
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function suppression()
    {
      $this->trace->fb('suppression : ' . $this->toStringClePrimaire());
      // Requete de suppression
      try
      {
        // Chargement des infos avant modification du  pari
        /*$pari = new Pari($this->maConnexion);
        $pari->setIdCompte($this->getIdCompte());
        $pari->setIdOperation($this->getIdOperation());
        $pari->affichage();
        
        // Modification du solde virtuel
        $compte = new Compte($this->maConnexion);
        $compte->setIdCompte($this->getIdCompte());
        $compte->affichage();
        
        // Retour en arrière sur le solde virtuel
        $compte->setSoldeVirtuel($compte->getSoldeVirtuel() - $pari->getMontantOperation());
        $compte->modification();*/
        
        // Suppression du pari
        $this->reqDelete->execute(array($this->IdOperation, $this->IdCompte));
        
        // Suppression de l'opération
        $operation = new Operation($this->maConnexion);
        $operation->setIdCompte($this->getIdCompte());
        $operation->setIdOperation($this->getIdOperation());
        $operation->suppression();
        
        $this->trace->fb('suppression réussie');          
      }
      catch(Exception $e)
      {
         //echo 'Erreur : <br>'; print_r($this->reqDelete->errorInfo());
         //throw new Exception('Une erreur est survenue lors de la suppression dans classe. Cause : ' . $e->getMessage() );
         throw new MyException('Une erreur est survenue lors de la suppression du pari. ', $this->reqDelete->errorInfo());
         
      }
    }

    private function ajouteOperation()
    {
      $this->trace->fb('ajouteOperation()');
      // Insertion dans Operation
      $operation = new Operation($this->maConnexion);
      $operation->setDate($this->getDate());
      $operation->setIdCompte($this->getIdCompte());
      $operation->setIdType(Operation::$ID_TYPE_PARI);
      $operation->setLibelle($operation->formatLibelle());
      $operation->setMontant($this->getMontantOperation());
      $operation->insertion();
      
      $this->trace->fb('$operation->getIdOperation() : ' . $operation->getIdOperation());
      // ajout de l'opération au pari
      $this->setIdOperation($operation->getIdOperation());
    }
    
    /*private function updateSoldeVirtuel()
    {
      $this->trace->fb('updateSoldeVirtuel()');
      // Modification du solde virtuel
      $compte = new Compte($this->maConnexion);
      $compte->setIdCompte($this->getIdCompte());
      $compte->affichage();
      $this->trace->fb('$compte->getSoldeVirtuel() : ' . $compte->getSoldeVirtuel());
      $compte->setSoldeVirtuel($compte->getSoldeVirtuel() + $this->getMontantOperation());
      $this->trace->fb('$compte->getSoldeVirtuel() : ' . $compte->getSoldeVirtuel());
      $compte->modification();        
    }*/
    /**
     * Short description of method select
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function select()
    {
      $this->trace->fb('Pari.select');
      $select = new Selector();
      $champs = array("IdOperation"=>$this->IdOperation, "IdCompte"=>$this->IdCompte, "Date"=>$this->Date, "IndSport"=>$this->IndSport, "Mise"=>$this->Mise, "Cote"=>$this->Cote, "IndResultat"=>$this->IndResultat, "IndCombine"=>$this->IndCombine, "NbPari"=>$this->NbPari, "Commentaire"=>$this->Commentaire, "SoldeVirtuel"=>$this->SoldeVirtuel);
      
      foreach($champs as $cle=>$valeur) 
      {
        // Les Champs
        $select->addChamp($cle);
        // Les Critères
        if( !empty($valeur) )
         $select->addCritere($cle, $valeur);
      }
      
      // La Table
      $select->addFrom('Pari');
      $this->trace->fb('Requete : ' . $select->toString());
      //echo 'requete : ' . $select->toString();
      return $this->maConnexion->query($select->toString());
    }

    /**
     * Short description of method selectNbParis
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  Integer iIdCompteP
     * @return mixed
     */
    private function selectNbParisOf( $iIdCompteP, $iMoisP)
    {
      $this->trace->fb('Pari.selectNbParis');
      $select = new Selector();

      // Les Champs
      $select->addChamp('Count(IdOperation)');

      // La Table
      $select->addFrom('Pari');

      // Les Critères
      $select->addCritere('IdCompte', $iIdCompteP);
      
      if( $iMoisP == 1 )
      {
        $dateDebut = date("Y-m-d", mktime(0, 0, 0, date("m"), 01, date("Y")));
        $dateMoisSuivant = date("Y-m-d", mktime(0, 0, 0, date("m")+1, 01, date("Y")));
        
        $select->addCritereSpecif('Date >= \''. $dateDebut . '\'');
        $select->addCritereSpecif('Date < \''. $dateMoisSuivant . '\'');
      }

      $this->trace->fb('Requete : ' . $select->toString());
      //  echo 'requete : ' . $select->toString();
      $stmt = $this->maConnexion->query($select->toString());
      $result = $stmt->fetch();
      return $result[0];
    }
    
    public function selectNbParis( $iIdCompteP)
    {
      return $this->selectNbParisOf( $iIdCompteP, 0);
    }
    
    public function selectNbParisOfMois( $iIdCompteP)
    {
      return $this->selectNbParisOf( $iIdCompteP, 1);
    }

    /**
     * Short description of method selectNbParisEncours
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  Integer iIdCompteP
     * @return mixed
     */
    public function selectNbParisEncours( $iIdCompteP)
    {
        $this->trace->fb('Pari.selectNbParisEncours(' . $iIdCompteP . ')');
        $lstParisEnCours = $this->selectParisEncours($iIdCompteP);
        
        return $lstParisEnCours->rowCount();     
    }
    
    /**
    * Short description of method selectParisEncours
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @param  Integer iIdCompteP
    * @return mixed
    */
    public function selectParisEncours( $iIdCompteP)
    {
      $this->trace->fb('Pari.selectParisEncours(' . $iIdCompteP . ')');
      $select = new Selector();
    
      // Les Champs
      $champs = array("IdOperation", "IdCompte", "Date", "IndSport", "Mise", "Cote", "IndResultat", "IndCombine", "NbPari", "Commentaire", "SoldeVirtuel");
      
      foreach($champs as $cle) 
      {
        // Les Champs
        $select->addChamp($cle);       
      }
    
      // La Table
      $select->addFrom('Pari');
    
      // Les Critères
      $select->addCritere('IdCompte', $iIdCompteP);
      $select->addCritereSpecif('IndResultat is null');
    
      $this->trace->fb('Requete : ' . $select->toString());
      //  echo 'requete : ' . $select->toString();
      return $this->maConnexion->query($select->toString());
    }

    /**
     * Short description of method selectAllParisDuMois
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  Integer iIdCompteP
     * @param  String sIndSportP
     * @return mixed
     */
    public function selectAllParisDuMois(  $iIdCompteP, $sIndSportP)
    {
        $this->trace->fb('Pari.selectAllParisDuMois(' . $iIdCompteP . ', ' . $sIndSportP . ')');
        $select = new Selector();
        
        // Les Champs
        $select->addChamp('IdOperation');
        $select->addChamp('IdCompte');
        $select->addChamp('Date');
        $select->addChamp('IndSport');
        $select->addChamp('Mise');
        $select->addChamp('Cote');
        $select->addChamp('IndResultat');
        $select->addChamp('IndCombine');
        $select->addChamp('NbPari');
        $select->addChamp('Commentaire');
        $select->addChamp('SoldeVirtuel');

        // La Table
        $select->addFrom('Pari');
        
        // Les Critères
        $select->addCritere('IdCompte', $iIdCompteP);
        $select->addCritere('IndSport', $sIndSportP);
        
        
        $dateDebut = date("Y-m-d", mktime(0, 0, 0, date("M"), 01, date("Y")));
        $dateMoisSuivant = date("Y-m-d", mktime(0, 0, 0, date("M")+1, 01, date("Y")));
        
        $select->addCritereSpecif('Date >= '. $dateDebut);
        $select->addCritereSpecif('Date < '. $dateMoisSuivant);
        
        $this->trace->fb('Requete : ' . $select->toString());
        //  echo 'requete : ' . $select->toString();
        return $this->maConnexion->query($select->toString());     
    }
    
    /**
     * Short description of method selectBenefice
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  Integer iIdPariP
     * @param  Integer iIdCompteP
     * @return mixed
     */
    public function selectBenefice( $iIdCompteP)
    {
        $this->trace->fb('Pari.selectBenefice(' . $iIdCompteP . ')');
        $stmt = $this->selectBenefPerte( $iIdCompteP, 0, Pari::$IND_RESULTAT_GAGNE);
      $result = $stmt->fetch();
      return $result[0]; 
    }
    
    public function selectBeneficeDuMois( $iIdCompteP)
    {
      $this->trace->fb('Pari.selectBeneficeDuMois(' . $iIdCompteP . ')');
      $stmt = $this->selectBenefPerte( $iIdCompteP, 1, Pari::$IND_RESULTAT_GAGNE);
      $result = $stmt->fetch();
      return $result[0];
    }
    /**
    * Short description of method selectBeneficeDuMois
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @param  Integer iIdPariP
    * @param  Integer iIdCompteP
    * @return mixed
    */
    private function selectBenefPerte( $iIdCompteP, $iMoisP, $sIndResultatP)
    {
      $this->trace->fb('Pari.selectBenefPerte(' . $iIdCompteP . ', ' . $iMoisP . ', ' . $sIndResultatP . ')');
      $select = new Selector();

      // Les Champs
      $select->addChamp('ROUND(SUM(Mise * (Cote - 1)), 2)');

      // La Table
      $select->addFrom('Pari');

      // Les Critères
      $select->addCritere('IdCompte', $iIdCompteP);
      $select->addCritere('IndResultat', $sIndResultatP);

      if( $iMoisP == 1 )
      {
        $dateDebut = date("Y-m-d", mktime(0, 0, 0, date("m"), 01, date("Y")));
        $dateMoisSuivant = date("Y-m-d", mktime(0, 0, 0, date("m")+1, 01, date("Y")));

        $select->addCritereSpecif('Date >= \''. $dateDebut . '\'');
        $select->addCritereSpecif('Date < \''. $dateMoisSuivant . '\'');
      }

      $this->trace->fb('Requete : ' . $select->toString());
      //  echo 'requete : ' . $select->toString();
      return $this->maConnexion->query($select->toString());
    }
    

    /**
     * Short description of method selectPerteDuMois
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @param  Integer iIdPariP
     * @param  Integer iIdCompteP
     * @return mixed
     */
    public function selectPerte( $iIdCompteP)
    {
      $this->trace->fb('Pari.selectPerte(' . $iIdCompteP . ')');
      $stmt = $this->selectBenefPerte( $iIdCompteP, 0, Pari::$IND_RESULTAT_PERDU);
      $result = $stmt->fetch();
      return $result[0];
    }
    public function selectPerteDuMois( $iIdCompteP)
    {
      $this->trace->fb('Pari.selectPerteDuMois(' . $iIdCompteP . ')');
      $stmt = $this->selectBenefPerte( $iIdCompteP, 1, Pari::$IND_RESULTAT_PERDU);
      $result = $stmt->fetch();
      return $result[0];
    }
    

    /**
    * Retourne le montant de tous les paris encours du compte
    * passé en paramètre
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @param  Integer iIdCompteP
    * @return mixed
    */
    public function selectMontantParisEncours( $iIdCompteP)
    {
      $this->trace->fb('Pari.selectBenefPerte(' . $iIdCompteP . ')');
      $select = new Selector();

      // Les Champs
      $select->addChamp('IdOperation');
      $select->addChamp('IdCompte');
      $select->addChamp('Mise');
      $select->addChamp('Cote');
      $select->addChamp('Date');
      $select->addChamp('IndSport');
      $select->addChamp('IndCombine');
      $select->addChamp('NbPari');
      $select->addChamp('Mise * (Cote - 1)');
      
      // La Table
      $select->addFrom('Pari');

      // Les Critères
      $select->addCritere('IdComp$parite', $iIdCompteP);
      $select->addCritereSpecif('IndResultat is null');

      $this->trace->fb('Requete : ' . $select->toString());
      //  echo 'requete : ' . $select->toString();
      return $this->maConnexion->query($select->toString());
    }
    
    public function selectLastPari($iIdCompteP)
    {
      $this->trace->fb('Pari.selectLastPari(' . $iIdCompteP . ')');
      
      $sousSelect = new Selector();
      $sousSelect->addChamp("MAX(idOperation)");
      $sousSelect->addFrom('Pari');
      $sousSelect->addCritere('IdCompte', $iIdCompteP);
      
      
      $select = new Selector();
      
      // Les Champs
      $select->addChamp('IdOperation');
      $select->addChamp('IdCompte');
      $select->addChamp('Mise');
      $select->addChamp('Cote');
      $select->addChamp('SoldeVirtuel');
      $select->addChamp('Date');
      $select->addChamp('IndSport');
      $select->addChamp('IndCombine');
      $select->addChamp('NbPari');
      $select->addChamp('IndResultat');
      //$select->addChamp('Mise * (Cote - 1)');
      
      // La Table
      $select->addFrom('Pari');
      
      // Les Critères
      $select->addCritere('IdCompte', $iIdCompteP);
      $select->addCritereSpecif('IdOperation = (' . $sousSelect->toStringSsRequete() . ')');
      
      $this->trace->fb('Requete : ' . $select->toString());
      //  echo 'requete : ' . $select->toString();
      return $this->maConnexion->query($select->toString());
    }
    
    /**
     * 
     * 
     * **/
    public function getMontantOperation()
    {
      $montant = -$this->Mise;
      if( $this->IndResultat == Pari::$IND_RESULTAT_GAGNE )
      {
        $montant = ($this->Mise * $this->Cote) - $this->Mise; 
      }
      else if( $this->IndResultat == Pari::$IND_RESULTAT_PERDU )
      {
        $montant = -$this->Mise;
      }
      else if( $this->IndResultat == Pari::$IND_RESULTAT_ANNULE )
      {
        $montant = 0;
      }
      return $montant;
    }
    
    public static function selectAllSports()
    {
       $allSports = array();
       $allSports[Pari::$IND_SPORT_FOOT]   = Pari::getLibelleSport(Pari::$IND_SPORT_FOOT);
       $allSports[Pari::$IND_SPORT_RUGBY]  = Pari::getLibelleSport(Pari::$IND_SPORT_RUGBY);
       $allSports[Pari::$IND_SPORT_TENNIS] = Pari::getLibelleSport(Pari::$IND_SPORT_TENNIS);
       $allSports[Pari::$IND_SPORT_BASKET] = Pari::getLibelleSport(Pari::$IND_SPORT_BASKET);
       return $allSports;
    }    
    
    public static function getLibelleSport($indSportP)
    {
      if( $indSportP == Pari::$IND_SPORT_FOOT )
      {
        return Pari::$IND_SPORT_FOOT_LIB;
      }
      if( $indSportP == Pari::$IND_SPORT_RUGBY )
      {
        return Pari::$IND_SPORT_RUGBY_LIB;
      }
      if( $indSportP == Pari::$IND_SPORT_TENNIS )
      {
        return Pari::$IND_SPORT_TENNIS_LIB;
      }
      if( $indSportP == Pari::$IND_SPORT_BASKET )
      {
        return Pari::$IND_SPORT_BASKET_LIB;
      }
    }
    
    public function selectMiseConseillee( $iCoteP )
    {
      if( $iCoteP == "" ) return "";
      
      $parametrage = new Parametrage($this->maConnexion);
      $parametrage->setIdCompte($this->IdCompte);
      $stmtParam =$parametrage->select();
      $resultParam = $stmtParam->fetch();
      
      $stmt = $this->selectLastPari($this->IdCompte);
      $result = $stmt->fetch();
    
      $miseConseillee = $resultParam['MiseMini'];
      
      if( $result['IndResultat'] == Pari::$IND_RESULTAT_PERDU || $result['IndResultat'] == "" )
      {
        $miseConseillee = ($result['SoldeVirtuel'] * ( $result['Cote'] - 1) + $result['Mise']) / ($iCoteP - 1);
        if( $resultParam['ModeCumule'] == 1 )
        {
          $miseConseillee += $resultParam['BeneficeMini'] / ($iCoteP - 1);
        }
      }
      else if( $result['IndResultat'] == Pari::$IND_RESULTAT_ANNULE )
      {
        $miseConseillee = $result['SoldeVirtuel'];
      }
      else if( $result['IndResultat'] == Pari::$IND_RESULTAT_GAGNE )
      {
        if( $result['SoldeVirtuel'] - $result['Mise'] <= 0 ) $miseConseillee = $resultParam['BeneficeMini'] / ($iCoteP - 1 );
        if( $result['SoldeVirtuel'] - $result['Mise'] > 0  ) $miseConseillee = $result['SoldeVirtuel'] - $result['Mise'];
      }
      
//      $miseConseillee = (-$result['SoldeVirtuel'] + $resultParam['BeneficeMini']) / ($iCoteP - 1 );
    
      if( $miseConseillee < $resultParam['MiseMini'] )
      {
        $miseConseillee = $resultParam['BeneficeMini'] / ($iCoteP - 1 );
        //$miseConseillee = ceil($result['BeneficeMini'] / ($iCoteP - 1 ) * 10 ) / 10;
        //         $miseConseillee = $result['MiseMini'];
      }
    
      $miseConseillee = ceil($miseConseillee * 10 ) / 10;
    
      return $miseConseillee;
    }
    
    /**
    * Short description of method setIdOperation
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @return mixed
    */
    public function setIdOperation($idOperationP)
    {
      $this->IdOperation = $idOperationP;
    }
    
    /**
     * Short description of method getIdOperation
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getIdOperation()
    {
      return $this->IdOperation;
    }
    
    /**
     * Short description of method setIdCompte
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setIdCompte($idCompteP)
    {
      $this->IdCompte = $idCompteP;
    }
    
    /**
     * Short description of method getIdCompte
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getIdCompte()
    {
      return $this->IdCompte;
    }
    
    /**
     * Short description of method setDate
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setDate($dDateP)
    {
      $this->Date = $dDateP;
    }
    
    /**
     * Short description of method getDate
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getDate()
    {
      return $this->Date;
    }
    
    /**
     * Short description of method setIndSport
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setIndSport($iIndSportP)
    {
      $this->IndSport = $iIndSportP;
    }
    
    /**
     * Short description of method getIndSport
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getIndSport()
    {
      return $this->IndSport;
    }
    
    /**
     * Short description of method setMise
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setMise($miseP)
    {
      $this->Mise = $miseP;
    }
    
    /**
     * Short description of method getMise
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getMise()
    {
      return $this->Mise;
    }
    
    /**
     * Short description of method setCote
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setCote($coteP)
    {
      $this->Cote = $coteP;
    }
    
    /**
     * Short description of method getCote
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getCote()
    {
      return $this->Cote;
    }
    
    /**
    * Short description of method setSoldeVirtuel
    *
    * @access public
    * @author firstname and lastname of author, <author@example.org>
    * @return mixed
    */
    private function setSoldeVirtuel($soldeVirtuelP)
    {
      $this->SoldeVirtuel = $soldeVirtuelP;
    }
    
    /**
     * Short description of method getSoldeVirtuel
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getSoldeVirtuel()
    {
      return $this->SoldeVirtuel;
    }
    
    
    /**
     * Short description of method setIndResultat
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setIndResultat($iIndResultatP)
    {
      $this->IndResultat = $iIndResultatP;
    }
    
    /**
     * Short description of method getIndResultat
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getIndResultat()
    {
      return $this->IndResultat;
    }
    
    /**
     * Short description of method setIndCombine
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setIndCombine($iIndCombineP)
    {
      $this->IndCombine = $iIndCombineP;
    }
    
    /**
     * Short description of method getIndCombine
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getIndCombine()
    {
      return $this->IndCombine;
    }
    
    /**
     * Short description of method setNbPari
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setNbPari($iNbPariP)
    {
      $this->NbPari = $iNbPariP;
    }
    
    /**
     * Short description of method getNbPari
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getNbPari()
    {
      return $this->NbPari;
    }
    
    /**
     * Short description of method setCommentaire
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function setCommentaire($sCommentaireP)
    {
      $this->Commentaire = $sCommentaireP;
    }
    
    /**
     * Short description of method getCommentaire
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function getCommentaire()
    {
      return $this->Commentaire;
    }
    
} /* end of class Pari */

?>